package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartall.R;
import com.example.smartall.adapter.HospitalCardListAdapter;
import com.example.smartall.adapter.HospitalCardRecycleAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.HospitalCardBean;
import com.example.smartall.entity.UserInfoBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.MApplication;
import com.example.smartall.utils.RecycleDecoration;
import com.example.smartall.utils.SPUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 个人信息卡
 */
public class HospitalPatientCardActivity extends BaseActivity {

    private HospitalCardBean.RowsBean userBean;

    private ImageView imBack;
    private TextView tvTitle;
    private RecyclerView rvCard;
    private CardView addCard;

    private List<HospitalCardBean.RowsBean> cardBean = new ArrayList<>();
    private HospitalCardRecycleAdapter adapter;

    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_patient_card);

        handler = new MHandler();

        init();
        initEvent();
        buildMineCard();
        getCardList();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        rvCard = (RecyclerView) findViewById(R.id.rv_card);
        addCard = (CardView) findViewById(R.id.add_card);

        tvTitle.setText("就诊卡");
        rvCard.setLayoutManager(new LinearLayoutManager(HospitalPatientCardActivity.this));
        rvCard.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = 10;
                outRect.bottom = 10;
            }
        });
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getThis().finish();
            }
        });

        addCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO
                migrateTo(CreatePatientCardActivity.class);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getCardList();
    }

    private void getCardList() {
        if (cardBean.size() > 0) return;
        Api.config(ApiConfig.HOSPITAL_MINE_CARD,null).getRequest(getThis(), new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HospitalCardBean bean = new Gson().fromJson(res, HospitalCardBean.class);
                if (bean != null && bean.getCode() == 200) {
                    cardBean = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },"");
    }

    private void buildMineCard() {
        SPUtils spUtils = SPUtils.config(HospitalPatientCardActivity.this, Constant.SP_USER_FILE_NAME);
        userBean = new HospitalCardBean.RowsBean();
        userBean.setName(spUtils.getString(Constant.USER_NAME));
        userBean.setSex(spUtils.getString(Constant.USER_SEX));
        userBean.setCardId(spUtils.getString(Constant.USER_ID_CARD));
        userBean.setTel(spUtils.getString(Constant.USER_PHONENUMBER));
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    cardBean.add(0, userBean);
                    adapter = new HospitalCardRecycleAdapter(HospitalPatientCardActivity.this, cardBean);
                    //创建或修改
                    adapter.setListener(new HospitalCardRecycleAdapter.OnClickListener() {
                        @Override
                        public void onClicked(int position) {
                            Intent intent = new Intent(HospitalPatientCardActivity.this, CreatePatientCardActivity.class);
                            intent.putExtra("cardInfo", cardBean.get(position));
                            if (position > 0) {
                                intent.putExtra("isChange", true);
                            }
                            startActivity(intent);
                        }
                    });
                    rvCard.setAdapter(adapter);
                    break;
            }
        }
    }
}